<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>新零售微店后台首页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/style/admin.css}" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="add" onclick="WinOpenAdd();">添加</button>
            </div>

            <table class="layui-hide" id="test"></table>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit" onclick="WinOpenEdit({{d.id}})" >编辑</a>
            </script>

        </div>
    </div>
</div>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/layui/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/shoppingApi/js/jquery-3.3.1.min.js}"></script>
<script type="application/javascript" th:src="@{/shoppingApi/js/common.js}"></script>
<!--<script th:src="@{/js/user/layuiuser.js}"></script>-->
<style>
    .layui-body{overflow-y: scroll;}
</style>
<script th:inline="none">
    var table;
    layui.use('table', function(){
        table = layui.table
            ,form = layui.form;

        table.render({
            elem: '#test'
            ,url:'/WeChat/JsonArticleList'
            ,cols: [[
                {type: 'checkbox'}
                ,{field:'id', title:'ID'}
                ,{field:'title', title:'标题'}
                ,{field:'description', title:'描述'}
                ,{field:'picurl', title:'图片链接'}
                ,{field:'url', title:'文章链接'}
                ,{field:'sort', title:'排序'}
                ,{field:'state', title: '状态'}
                ,{field:'id', title:'编辑', templet: '#barDemo', unresize: true}
            ]]
            ,id: 'testReload'
        });

        //监听表格复选框选择
        table.on('checkbox(demo)', function(obj){
            console.log(obj)
        });


    });





    function createTime(v){
         return new Date(v).getFullYear()+"-"+new Date(v).getMonth()+"-"+new Date(v).getDay()+" "+new Date(v).getHours()+":"+new Date(v).getMinutes()+":"+new Date(v).getSeconds();
    }
    /**重写toLocaleString方法*/
    Date.prototype.toLocaleString = function() {
        var y = this.getFullYear();
        var m = this.getMonth()+1;
        m = m<10?'0'+m:m;
        var d = this.getDate();
        d = d<10?("0"+d):d;
        var h = this.getHours();
        h = h<10?("0"+h):h;
        var M = this.getMinutes();
        M = M<10?("0"+M):M;
        var S=this.getSeconds();
        S=S<10?("0"+S):S;
        return y+"-"+m+"-"+d+" "+h+":"+M+":"+S;}


    function WinOpenAdd() {
        layer.open({
            type: 2
            , title: '添加公众号文章'
            , content: '/WeChat/Article/Add'
            , area: ['420px', '420px']
            , btn: ['确定', '取消']
            , yes: function (index, layero) {
                var body = layer.getChildFrame('body', index); //得到iframe页的body内容
                var title = body.find("#title").val();
                var description = body.find("#description").val();
                var picUrl = body.find("#picUrl").val();
                var url = body.find("#url").val();
                var sort = body.find("#sort").val();

                var addUrl="/WeChat/Article/AddArticle?title="+title+"&description="+description+"&picUrl="+picUrl+"&url="+url+"&sort="+sort;
                //alert(addUrl);
                $.ajax({
                    url:addUrl,
                    async:false,
                    success:function (res) {
                        if(res.result=='1'){}
                    }
                });

                //数据刷新
                var demoReload = $('#demoReload');
                //执行重载
                table.reload('testReload', {
                });

                layer.close(index); //关闭弹层
            }
        });
    }

    function WinOpenEdit(id) {
        layer.open({
            type: 2
            , title: '编辑公众号文章'
            , content: '/WeChat/Article/Edit?id='+id
            , area: ['420px', '420px']
            , btn: ['确定', '取消']
            , yes: function (index, layero) {
                var body = layer.getChildFrame('body', index); //得到iframe页的body内容
                var title = body.find("#title").val();
                var description = body.find("#description").val();
                var picUrl = body.find("#picUrl").val();
                var url = body.find("#url").val();
                var sort = body.find("#sort").val();

                var editUrl="/WeChat/Article/EditArticle?id="+id+"&title="+title+"&description="+description+"&picUrl="+picUrl+"&url="+url+"&sort="+sort;
                //alert(editUrl);
                $.ajax({
                    url:editUrl,
                    async:false,
                    success:function (res) {
                        if(res.result=='1'){}
                    }
                });
                //执行重载
                table.reload('testReload', {

                });

                layer.close(index); //关闭弹层
            }
        });
    }

</script>


</body>
</html>